#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<tuple>
using namespace std;

const int N = 1e5 + 10, M = 5e5 + 10, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx;
#define x first
#define y second
typedef tuple<int, int, int> PII;
int ty[N];
int dis[N][4];
bool st[N][4];
int n, m, s, t;
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void dij(int s)
{
    memset(dis, 0x3f, sizeof dis);
    priority_queue<PII, vector<PII>, greater<PII>> heap;

    if (ty[s] != 2) {
        heap.push({ 0, s, ty[s] });
        dis[s][ty[s]] = 0;
    }
    else return;
    while (!heap.empty()) {
        auto [dist, ver, type] = heap.top(); heap.pop();
        if (st[ver][type]) continue;
        st[ver][type] = 1;
        for (int i = h[ver]; ~i; i = ne[i])
        {
            int j = e[i];
            if (ty[j] == 2) {
                if (type == 1) {
                    if (dis[j][3] > dis[ver][1] + w[i]) {
                        dis[j][3] = dis[ver][1] + w[i];
                        heap.push({ dis[j][3], j, 3 });
                    }
                }
            }
            else {
                if (ty[j] & type) continue;
                if (dis[j][ty[j] | type] > dis[ver][type] + w[i]) {
                    dis[j][ty[j] | type] = dis[ver][type] + w[i];
                    heap.push({ dis[j][ty[j] | type], j, ty[j] | type });
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d", &n, &m, &s, &t);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &ty[i]);
        if (ty[i] == 3) ty[i] = 0;
    }
    memset(h, -1, sizeof h);
    for (int i = 1; i <= m; i++) {
        int a, b, m;
        scanf("%d%d%d", &a, &b, &m);
        add(a, b, m);
    }
    dij(s);
    if (dis[t][3] == 0x3f3f3f3f) dis[t][3] = -1;
    printf("%d\n", dis[t][3]);
}